//ask是是否要进行询问授权，true为要，false为不要
//sessionChoose为1,2,3,4,所以paramSession下标为0的则为空

// const baseUrl = "https://m4.xiaoyu.com/index.php/jjd/";

import API from './config.js'

function HttpRequst(loading, url, sessionChoose, sessionId, params, method, ask, callBack,isForceCallBack) {
  if (loading == true) {
    wx.showToast({
      icon: 'loading',
      duration:15000
    })
  }
  var params = Object.prototype.toString.call(params) == "[object Object]" ? params : {};
  params["ukey"] = sessionId;
  var paramSession = sessionChoose == 1 ? {
    'content-type': 'application/x-www-form-urlencoded'
  } : {
    'content-type': 'application/json'
  };

  wx.request({
    url: API.baseUrl + url,
    data: params,
    dataType: "json",
    header: paramSession,
    method: method,
    success: function(res) {
      if (loading == true) {
        wx.hideToast(); //隐藏提示框
      }
      if (res.data.code == 403 && isForceCallBack !="index") {
        wx.showModal({
          title: '提示',
          content: res.data.msg ? res.data.msg : "",
          cancelText: "我知道了",
          confirmText: "立刻登录",
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/login',
              })
            }
          }
        });
        return;
      } else if (res.data.code == 400) {
        setTimeout(function() {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        });
        if (!isForceCallBack){
          return;
        }
      }
      callBack(res.data);
    },
    complete: function() {
      if (loading == true) {
        wx.hideToast(); //隐藏提示框
      }
    }
  })
}




module.exports = HttpRequst